package com.yeyks.task;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.yeyks.common.constants.activity.RedisKeyPreConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author shuai
 * @date 2019/11/07 19:15
 */
@Component
@Slf4j
public class ActivityDoubleDanTask {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 每天凌晨5分执行一次
     */
    @Scheduled(cron = "${schedule.activity.doubleDan}")
    private void activityDoubleDanTask() {
        log.info("活动双旦定时任务开始-----------，time = {}", DateUtil.formatDateTime(new Date()));
        List<String> keys = getDoubleDanCountByKey();
        for (String key : keys) {
            redisTemplate.delete(key);
        }
        log.info("活动双旦定时任务结束");
    }

    private List<String> getDoubleDanCountByKey() {
        List<String> lists = Lists.newArrayList();
        lists.add(RedisKeyPreConstant.STATISTICAL_DATA_PRE + RedisKeyPreConstant.STATISTICAL_DATA_MIDDLE_ACTIVITY + RedisKeyPreConstant.STATISTICAL_DATA_MIDDLE_ACTIVITY_END_DOUBLEDAN_LOTTERY_COUNT);
        lists.add(RedisKeyPreConstant.STATISTICAL_DATA_PRE + RedisKeyPreConstant.STATISTICAL_DATA_MIDDLE_ACTIVITY + RedisKeyPreConstant.STATISTICAL_DATA_MIDDLE_ACTIVITY_END_DOUBLEDAN_LOTTERY_ALLOW1);
        lists.add(RedisKeyPreConstant.STATISTICAL_DATA_PRE + RedisKeyPreConstant.STATISTICAL_DATA_MIDDLE_ACTIVITY + RedisKeyPreConstant.STATISTICAL_DATA_MIDDLE_ACTIVITY_END_DOUBLEDAN_LOTTERY_ALLOW2);
        return lists;
    }
}
